﻿#include <QtWidgets>
#include <QSettings>
#include <QTranslator>
#include <QQuickView>
#include <QQmlContext>
#include <QQmlApplicationEngine>
#include "mainwindow.h"
#include "FrameLessView.h"
#include "stockmodel.h"

MainWindow::MainWindow(QObject *parent)
    : QObject(parent)
    , m_mainManager(new MainManager(this))
    , m_stockListModel(new StockListModel(this))
    , m_translator(new QTranslator(this))
    , m_engine(new QQmlApplicationEngine(this))
{
    initLanguage();
    initFont();
    initUI();
}

MainWindow::~MainWindow()
{
}


void MainWindow::start()
{
    m_engine->load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
//    QObject::connect(m_engine->, &QQuickView::statusChanged, &m_view, [this](QQuickView::Status status) {
//        if (status == QQuickView::Status::Ready) {
//            m_qmlFactory.qmlLoadFinish();
//        }
//    });
    QUrl url(QStringLiteral("qrc:/qml/main.qml"));
    // move to center
//    QRect screenGeometry = QGuiApplication::primaryScreen()->geometry();
//                this->setGeometry(screenGeometry.width() / 2 - width() / 2, \
//                    screenGeometry.height() / 2 - height() / 2, width(), height());
//    show();
}

void MainWindow::initUI()
{
    //setTitle("feedback");
    m_engine->rootContext()->setContextProperty("view", this);
    m_engine->rootContext()->setContextProperty("stockListModel", m_stockListModel);
    m_engine->rootContext()->setContextProperty("mainManager", m_mainManager);
    connect(m_stockListModel, &StockListModel::signalHold, m_mainManager, &MainManager::signalHold);
    connect(m_stockListModel, &StockListModel::signalCurrentCode, m_mainManager, &MainManager::signalCurrentCode);
}

void MainWindow::initLanguage()
{
    qApp->removeTranslator(m_translator);
    QString cfgPath = QCoreApplication::applicationDirPath() + "/global_config.ini";
    m_setting = new QSettings(cfgPath, QSettings::IniFormat, this);
    m_setting->beginGroup(LAUNCH_GROUP);
    int language = m_setting->value(KEY_LAUNCH_LANGUAGE, 0).toInt();
    m_setting->endGroup();

    if(language == 1) {
        m_translator->load(":/languages/languages/translation_zh.qm", ".");
    }else if(language == 0){
        m_translator->load(":/languages/languages/translation_en.qm", ".");
    }

    qApp->installTranslator(m_translator);
    m_engine->rootContext()->engine()->retranslate();
}


void MainWindow::initFont()
{
    int a = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-Thin.otf");
    a = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-45-Light.otf");
    a = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-55-Regular.otf");
    a = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-55-RegularL3.otf");
    a = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-65-Medium.otf");
    a = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-75-SemiBold.otf");
    a = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-85-Bold.otf");
    a = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-95-ExtraBold.otf");
    a = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-105-Heavy.otf");
    a = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-115-Black.otf");
    QString sansCNFamily = "Alibaba PuHuiTi 3.0";
    QFont defaultFont(sansCNFamily);
    QApplication::setFont(defaultFont);
}


